package leaveSystem.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import leaveSystem.bizlogic.ViewLeaveManager;
import leaveSystem.model.Employee;

/**
 * Servlet implementation class EditLeaveServlet
 */
@WebServlet("/editleave")
public class EditLeaveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EditLeaveServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws NumberFormatException,
			ServletException, IOException {
		
		 HttpSession session=request.getSession(); 
		 Employee emp=(Employee) session.getAttribute("currentSessionUser");
		 int staffid=emp.getEmployeeId();
		 String staffname=emp.getName();		 

		ViewLeaveManager manager = new ViewLeaveManager();
		
		int leaveid = Integer.parseInt(request.getParameter("leaveid"));
		String button = request.getParameter("button");
		if(button.equalsIgnoreCase("Cancel")){
			button="Cancelled";
		}
		else if(button.equalsIgnoreCase("Delete")){
			button="Deleted";
		}
		else{
			button="Updated";
		}
			
		String forward = "";

		if (button.equalsIgnoreCase("Updated")) {
			request.setAttribute("leaveid", leaveid);
			forward = "/ApplyLeaveServlet";
		} else {
			manager.submitDecision(staffname, leaveid, button, null);
			forward = "/viewpersonalleave";
		}
		RequestDispatcher rd = request.getRequestDispatcher(forward);
		rd.forward(request, response);
	}

}
